# +----------------------------------------------------------------------
# | ChatWork
# +----------------------------------------------------------------------
# | 这不是一个自由软件,您只能在不用于商业目的的前提下对程序代码进行修改和使用。
# | 任何企业和个人不允许对程序代码以任何形式任何目的再发布,商业使用请获取授权。
# +----------------------------------------------------------------------
# | Author: ChatWork Team <2474369941@qq.com>
# +----------------------------------------------------------------------
from typing import Union, List, Any, Dict
from decimal import Decimal
from fastapi import Query
from pydantic import BaseModel, Field


class RobotSmarterSearchIn(BaseModel):
    """ 智能体搜索参数 """
    page_no: int = Query(default=1, gt=0, description="当前页码")
    page_size: int = Query(default=15, gt=0, le=200, description="每页条数")
    owner: Union[str, None] = Query(default="", description="拥有者: [all=全部, private=私有, cooperate=协作]")
    keyword: Union[str, None] = Query(default=None, description="关键词")


class RobotSmarterDetailIn(BaseModel):
    """ 智能体详情参数 """
    code: str = Query(..., min_length=1, max_length=32, description="智能体编号")


class RobotSmarterAddIn(BaseModel):
    """ 智能体新增参数 """
    name: str = Field(..., min_length=1, max_length=100, description="智能体名称")
    intro: str = Field(default="", max_length=800, description="智能体简介")

    @classmethod
    def messages(cls):
        return {
            "name.min_length": "请设置智能体名称",
            "name.max_length": "智能体名称不能超出32个字符",
            "intro.max_length": "智能体简介不能超出800个字符"
        }

    class Config:
        json_schema_extra = {
            "example": {
                "logo": "https://www.xx.com/aa.png",
                "name": "通用智能助手",
                "intro": "智能体简介"
            }
        }


class RobotSmarterEditIn(BaseModel):
    """ 智能体编辑参数 """
    code: str = Field(..., min_length=1, max_length=32, description="智能体编号")
    logo: str = Field(..., min_length=1, max_length=250, description="智能体图标")
    name: str = Field(..., min_length=1, max_length=32, description="智能体名称")
    intro: str = Field(default="", max_length=800, description="智能体简介")
    search_mode: str = Field(default="similar", pattern=r"^(similar|full|mix)$", description="检索的模式")
    search_tokens: int = Field(default=5000, ge=0, le=50000, description="检索引用上限")
    search_similar: Decimal = Field(default=0.4, le=1, max_digits=4, decimal_places=3, description="检索相似: [0.0~1.0]")
    ranking_status: int = Field(default=0, ge=0, le=1, description="检索的重排: [0=否, 1=是]")
    optimize_ask: int = Field(default=0, ge=0, le=1, description="问题的优化: [0=否, 1=是]")
    annex_similar: Decimal = Field(default=0.4, le=1, max_digits=4, decimal_places=3, description="附件相似: [0.0~1.0]")
    ranking_score: Decimal = Field(default=0.4, le=1, max_digits=4, decimal_places=3, description="重排: [0.1~1.0]")
    ranking_model: str = Field(default="", max_length=100, description="重排模型")
    optimize_model: str = Field(default="", max_length=100, description="优化模型")
    prompt_roles: str = Field(default="", max_length=65535, description="角色提示词")
    prompt_limit: str = Field(default="", max_length=65535, description="限定提示词")
    welcome_text: str = Field(default="", max_length=65535, description="欢迎光临词")
    question_guide: int = Field(default=0, ge=0, le=1, description="猜您想问的")
    empty_search_text: str = Field(default="", max_length=65535, description="空搜索文本")
    empty_search_type: str = Field(default="ai", pattern=r"^(ai|default)$", description="空搜索类型: [ai, default]")
    max_upload_num: int = Field(..., ge=0, le=15, description="最大上传数: [0-15]")
    is_upload_files: int = Field(..., ge=0, le=1, description="文档上传: [0=否, 1=是]")
    is_upload_image: int = Field(..., ge=0, le=1, description="图片上传: [0=否, 1=是]")
    is_show_timed: int = Field(..., ge=0, le=1, description="显示耗时: [0=否, 1=是]")
    is_show_quote: int = Field(..., ge=0, le=1, description="显示引用: [0=否, 1=是]")
    is_show_context: int = Field(..., ge=0, le=1, description="显上下文: [0=否, 1=是]")
    is_original_reply: int = Field(..., ge=0, le=1, description="原文答复: [0=否, 1=是]")
    llm_models: str = Field(..., min_length=1, max_length=100, description="对话的模型")
    llm_config: dict = Field(default={}, description="模型的参数")
    broadcast: Dict[str, Union[str, int, float, list]] = Field(description="语音播报参")
    knows: List[str] = Field(default=[], description="关联知识库")
    mcps: List[str] = Field(default=[], description="关联MCP")

    @classmethod
    def messages(cls):
        return {
            "code.min_length": "智能体编号参数缺失",
            "code.max_length": "智能体编号参数错误",
            "logo.min_length": "请设置智能体图标",
            "logo.max_length": "智能体图标路径异常",
            "name.min_length": "请设置智能体名称",
            "name.max_length": "智能体名称不能超出32个字符",
            "intro.max_length": "智能体简介不能超出800个字符",
            "llm_models.min_length": "请设置对话模型",
            "search_mode.pattern": "检索模式不是合法值: [similar, full, mix]",
            "search_tokens.ge": "检索引用上限不能少于0",
            "search_tokens.le": "检索引用上限不能大于50000",
            "search_similar.le": "检索相似度不能大于0.0",
            "search_similar.max_digits": "检索相似度整数不能超出1位",
            "search_similar.decimal_places": "检索相似度小数不能超出3位",
            "annex_similar.max_digits": "附件检索相似度整数不能超出1位",
            "annex_similar.decimal_places": "附件检索相似度小数不能超出3位",
            "empty_search_type.pattern": "空搜索类型不是合法值: [ai,default]"
        }

    class Config:
        json_schema_extra = {
            "example": {
                "code": "HCxiS50f0d4nd",
                "type": "simple",
                "logo": "https://www.waitadmin.cn/images/logo.png",
                "name": "智能体名称",
                "intro": "智能体简介",
                "owned": True,
                "power": 3,
                "search_mode": "similar",
                "search_tokens": 5000,
                "search_similar": 0.4,
                "ranking_status": 0,
                "optimize_ask": 0,
                "annex_similar": 0.4,
                "ranking_score": 0,
                "ranking_model": "qianfan:bce-reranker-base",
                "prompt_roles": "",
                "prompt_limit": "",
                "welcome_text": "",
                "empty_search_text": "",
                "empty_search_type": "ai",
                "question_guide": 0,
                "max_upload_num": 0,
                "is_upload_files": 0,
                "is_upload_image": 0,
                "is_show_timed": 0,
                "is_show_quote": 0,
                "is_show_context": 0,
                "is_original_reply": 0,
                "llm_name": "OpenAI",
                "llm_models": "openai:gpt-3.5-turbo-0125",
                "llm_config": [
                    {
                        "key": "context_num",
                        "name": "context",
                        "type": "slider",
                        "default": 3,
                        "range": [0, 10],
                        "step": 1,
                        "tips": "Control the number of contexts"
                    }
                ],
                "knows": [
                    {
                        "code": "wZS3mj23aj6kP",
                        "logo": "https://www.x.cn/images/legal.png",
                        "name": "知识库名称",
                        "vector_name": "OpenAI",
                        "vector_model": "openai:text-embedding-ada-002",
                        "is_disable": 0
                    }
                ],
                "mcps": [
                    {
                        "code": "wZS3mj23aj6kP",
                        "logo": "https://www.x.cn/images/legal.png",
                        "name": "MCP",
                        "is_disable": 0
                    }
                ],
                "broadcast": {
                    "model": "xunfei:x4_lingxiaoxuan_oral",
                    "speed": 0.25,
                    "speed_range": [0.1, 10, 0.1]
                }
            }
        }


class RobotSmarterCodeIn(BaseModel):
    """ 智能体编号参数 """
    code: str = Field(..., min_length=1, max_length=32, description="智能体编号")

    @classmethod
    def messages(cls):
        return {
            "code.min_length": "智能体编号参数缺失",
            "code.max_length": "智能体编号参数错误"
        }


"""--------------- Separator ---------------"""


class RobotSmarterListVo(BaseModel):
    """ 智能体列表VO """
    code: str = Field(description="智能体编号")
    logo: str = Field(description="智能体图标")
    name: str = Field(description="智能体名称")
    intro: str = Field(description="智能体简介")
    power: int = Field(description="拥有的权限")
    is_owner: bool = Field(description="是否私有: [true=私有, false=协作]")
    is_square: int = Field(description="广场公开: [-1=未公开,0=待审核,1=公开,2=拒绝]")
    create_time: str = Field(description="创建时间")
    update_time: str = Field(description="更新时间")
    models: Dict[str, str] = Field(description="模型信息")
    user: Dict[str, str] = Field(description="所属用户")

    class Config:
        json_schema_extra = {
            "example": {
                "code": "HCxiS50f0d4nd",
                "logo": "https://www.x.cn/images/logo.png",
                "name": "智能体名称",
                "intro": "智能体简介",
                "power": 4,
                "is_owner": True,
                "is_square": -1,
                "create_time": "08/07 17:53",
                "update_time": "08/07 17:53",
                "models": {
                    "name": "OpenAI",
                    "model": "gpt35-turbo-0125"
                },
                "user": {
                    "sn": "80965941",
                    "nickname": "wait",
                    "avatar": "https://www.x.cn/images/avatar.png"
                }
            }
        }


class RobotSmarterDetailVo(BaseModel):
    """ 智能体详情VO """
    code: str = Field(description="智能体编号")
    type: str = Field(description="智能体类型")
    logo: str = Field(description="智能体图标")
    name: str = Field(description="智能体名称")
    intro: str = Field(description="智能体简介")
    owned: bool = Field(description="是否拥有者: [true=拥有者, false=协作者]")
    power: int = Field(description="拥有的权限: [1=查看者, 2=编辑者, 3=管理者, 4=拥有者]")
    search_mode: str = Field(description="检索的模式: [语义, 全文, 混合]")
    search_tokens: int = Field(description="检索引用上限: [0 ~ 30000]")
    search_similar: Decimal = Field(description="检索相似度: [0 ~ 1]")
    ranking_status: int = Field(description="检索的重排: [0=否, 1=是]")
    optimize_ask: int = Field(description="问题的优化: [0=否, 1=是]")
    annex_similar: Decimal = Field(description="附件相似度")
    ranking_score: Decimal = Field(description="重排分数")
    ranking_model: str = Field(description="重排模型")
    optimize_model: str = Field(description="优化模型")
    prompt_roles: str = Field(description="角色提示词")
    prompt_limit: str = Field(description="限定提示词")
    welcome_text: str = Field(description="欢迎光临词")
    question_guide: int = Field(description="猜您想问的")
    empty_search_text: str = Field(description="空搜索文本")
    empty_search_type: str = Field(description="空搜索类型: [ai, default]")
    max_upload_num: int = Field(description="最大上传数")
    is_upload_files: int = Field(description="文档上传: [0=否, 1=是]")
    is_upload_image: int = Field(description="图片上传: [0=否, 1=是]")
    is_web_search: int = Field(description="联网搜索: [0=否, 1=是]")
    is_show_timed: int = Field(description="显示耗时: [0=否, 1=是]")
    is_show_quote: int = Field(description="显示引用: [0=否, 1=是]")
    is_show_context: int = Field(description="显上下文: [0=否, 1=是]")
    is_original_reply: int = Field(description="原文答复: [0=否, 1=是]")
    llm_name: str = Field(description="模型名称")
    llm_models: str = Field(description="对话的模型")
    llm_config: List[dict] = Field(description="对话的模型")
    broadcast: Dict[str, Union[str, int, float, list]] = Field(description="语音播报参")
    knows: List[Any] = Field(description="关联知识库")
    mcps: List[Any] = Field(description="关联MCP")

    class Config:
        json_schema_extra = {
            "example": {
                "code": "HCxiS50f0d4nd",
                "type": "simple",
                "logo": "https://www.x.cn/images/logo.png",
                "name": "智能体名称",
                "intro": "智能体简介",
                "owned": True,
                "power": 4,
                "search_mode": "similar",
                "search_tokens": 5000,
                "search_similar": 0.4,
                "ranking_status": 0,
                "optimize_ask": 0,
                "annex_similar": 0.4,
                "ranking_score": 0.4,
                "ranking_model": "",
                "prompt_roles": "",
                "prompt_limit": "",
                "welcome_text": "",
                "question_guide": 0,
                "empty_search_text": "",
                "empty_search_type": "ai",
                "max_upload_num": 0,
                "is_upload_files": 0,
                "is_upload_image": 0,
                "is_web_search": 0,
                "is_show_timed": 0,
                "is_show_quote": 0,
                "is_show_context": 0,
                "is_original_reply": 0,
                "llm_name": "OpenAI",
                "llm_models": "openai:gpt-3.5-turbo-0125",
                "llm_config": [
                    {
                        "key": "context_num",
                        "name": "context",
                        "type": "slider",
                        "default": 3,
                        "range": [0, 10],
                        "step": 1,
                        "tips": "Control the number of contexts"
                    }
                ],
                "knows": [
                    {
                        "code": "wZS3mj23aj6kP",
                        "logo": "https://www.x.cn/images/legal.png",
                        "name": "知识库名称",
                        "vector_name": "OpenAI",
                        "vector_model": "openai:text-embedding-ada-002",
                        "is_disable": 0
                    }
                ],
                "mcps": [
                    {
                        "code": "wZS3mj23aj6kP",
                        "logo": "https://www.x.cn/images/legal.png",
                        "name": "mcp",
                        "is_disable": 0
                    }
                ],
                "broadcast": {
                    "model": "xunfei:x4_lingxiaoxuan_oral",
                    "speed": 0.25,
                    "speed_range": [0.1, 10, 0.1]
                }
            }
        }
